import java.util.*;

public class testwork5 {
    public static List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> map1=new HashMap<>();
        int x=0;
        for(String s:words){
            if(!map1.containsKey(s)){
                map1.put(s,1);
                x++;
            }else{
                map1.put(s,map1.get(s)+1);
            }
        }
      Integer []sum=new Integer[x];
        int i=0;
        for(Map.Entry<String, Integer> integerMap:map1.entrySet()){
            sum[i]= integerMap.getValue();
            i++;
        }
      Arrays.sort(sum,Collections.reverseOrder());
        int temp=sum[k-1];
        List<String>list=new ArrayList<>();
        for(Map.Entry<String, Integer> integerMap:map1.entrySet()){
            if(integerMap.getValue()>= temp){
                list.add(integerMap.getKey());
            }
        }
        return list;
    }

    public static void main(String[] args) {
        String[]arr={"i","love","leetcode","i","love","coding"};
        List<String>list=new ArrayList<>();
        list=topKFrequent(arr,2);
        System.out.println(list.toString());
    }
}
